package dongtai;public class siliuqi2 {
    public static int findSubstringInWraproundString(String s) {
        int n = s.length();
        if (n == 0) return 0;
        if (n == 1) return 1;

        s = " " + s;
        // 将字符串数组转化为字符数组
        char[] charArray = s.toCharArray();

        // 获取字符数组的ascii值（需要实际赋值）
        int[] asciiArray = new int[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            asciiArray[i] = (int) charArray[i];
        }

        int[] f = new int[n + 1];
        f[0] = 0;

        // 新增：用于去重的数组，记录以每个字符结尾的最长子串长度
        int[] maxLen = new int[26];

        for (int i = 1; i < n + 1; i++) {
            if ((charArray[i - 1] == 'z' && charArray[i] == 'a') ||
                    asciiArray[i - 1] + 1 == asciiArray[i]) {
                f[i] = f[i - 1] + 1;
            } else {
                f[i] = 1;
            }

            // 新增：更新以当前字符结尾的最长子串长度（去重关键）
            int charIndex = charArray[i] - 'a';
            if (f[i] > maxLen[charIndex]) {
                maxLen[charIndex] = f[i];
            }
        }

        // 修改：不再返回f[n]，而是返回所有字符最大长度的和
        int sum = 0;
        for (int len : maxLen) {
            sum += len;
        }
        return sum;
    }

    public static void main(String[] args) {
        String s = "cac";
        int substringInWraproundString = findSubstringInWraproundString(s);
        System.out.println(substringInWraproundString); // 输出: 2
    }
}
